<template>
	<view class="content">
		<image class="loginIc" :src="localImages +'/loginBac.png'" mode="widthFix"></image>
		<image class="loginTil" :src="localImages +'/loginTil.png'" mode="widthFix" @click="doubleClick"></image>
		<view class="handler">
			<button class="btOne btDefalt" @click="returnMain">取消</button>
			<button class="btOne" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信登录</button>
		</view>
		<modal v-if="showModal" title="开启调试模式" confirm-text="确定" cancel-text="取消" @cancel="cancel" @confirm="confirm">
			<input class="modalInput" placeholder-class="inputPlaceHold" v-model="secrect" placeholder="请输入调试密码" />
		</modal>
	</view>
</template>

<script>
	import config from '../../static/config.js'
	import util from '../../static/utils/util.js'
	export default {
		data() {
			return {
				images: config.images,
				localImages: config.localImages,
				title: '123',
				showModal: 0,
				previous: '',
				secrect: '',
			}
		},
		onLoad() {
			//在登录界面先获取下session信息，避免登录时，后台无法获取手机号
			this.getOpenId();
		},
		methods: {
			//0.3s内双击
			doubleClick() {
				var now = new Date();
				if (now - this.previous < 300) {
					this.showModal = true
				}
				this.previous = now;
			},
			confirm() {
				if (this.secrect == '666') {
					// this.showModal = false
					wx.navigateTo({
						url: '/pages/index/test',
					})
				} else {
					wx.showToast({
						title: '密码错误',
						icon: 'none',
						duration: 1000
					})
				}

			},
			cancel() {
				console.log('cancel')
				this.showModal = false
			},
			returnMain() {
				wx.switchTab({
					url: '/pages/index/index'
				})
			},
			async getOpenId() {
				var res = await this.wxLogin();
				//通过 wx.login 接口获得临时登录凭证 code 授权
				var jsCode = res.code
				var url = '/UserServer/mini/install/getOperationWxCode2Session'
				var params = {
					js_code: jsCode
				}
				var options = {
					url,
					params
				}
				var param = await util.requestPro(options)
				wx.setStorageSync('openId', param.openid);
			},
			wxLogin() {
				return new Promise((resolve, reject) => {
					wx.login({
						complete: (res) => {
							resolve(res)
						},
					})
				})
			},
			//获取用户手机号码
			async getPhoneNumber(userInfo) {
				let self = this;
				console.warn('userInfo', userInfo);
				var phoneInfo = userInfo.detail
				if (!phoneInfo.iv) {
					wx.showToast({
						title: '请允许授权登录',
						icon: 'none',
						duration: 1000
					})
					return
				}
				wx.removeStorageSync('token') //移除token
				var res = await self.wxLogin();
				var jsCode = res.code
				var url = '/UserServer/weChat/getWxUserOpenId'
				var url = '/UserServer/mini/install/getWxUserOpenId'
				var params = {
					encryptedData: phoneInfo.encryptedData,
					iv: phoneInfo.iv,
					js_code: jsCode
				}
				var options = {
					url,
					params
				}
				var param = await util.requestPro(options)
				console.log('param', param)
				if (param) {
					var phoneNumber = param.phoneNumber;
					// var phoneNumber=18570375317;//测试登录手机号
					if (phoneNumber) {
						wx.setStorageSync('phone', phoneNumber);
						wx.navigateTo({
							url: '/pages/index/select'
						})
					} else if (param.statusCode == 200) {
						console.log('param', param.statusCode == 200)
						wx.showToast({
							title: '请重新获取手机号',
							icon: 'none'
						})
					}
				}
			}

		},

	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.loginIc {
		width: 420rpx;
		margin-top: 200rpx;
		margin-bottom: 50rpx;
	}

	.loginTil {
		width: 528rpx;
	}

	.handler {
		display: flex;
		justify-content: space-between;
		width: 100%;
		margin-top: 300rpx;
	}

	.btOne {
		width: 330rpx;
		font-weight: bold;
	}

	.btOne.btDefalt {
		background: #DDDDDD;
		color: #2035E0;
		box-shadow: 0px 13rpx 29rpx 0px #DDDDDD;
	}

	.modalInput {
		text-align: center;
	}
</style>
